#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "sys.h"
#include "com.h"
#include "mhbtn.h"
#include "reset.h"


/*
 *
 */
static void CreateResetInterface(HWND hWnd)
{
    char esc[64] = {0};
    sprintf(esc, GetString(802), 5);
    const char *sc[5] = {
        GetString(707),
        GetString(703),
        GetString(704),
        GetString(705),
        esc,
    };
    int id[5] = {
        ID_BTN_RESET,
        ID_BTN_HD,
        ID_GOTO_LINE,
        ID_DROP,
        ID_ESC,
    };
    int i = 0;
    for(i = 0; i < 5; ++i)
    {
        CreateWindow(MH_BTN_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE | BS_FLAT | BS_LEFT,
            id[i],
            20,
            60 + i * 40,
            160,
            20,
            hWnd, MH_BTN_BKGND_BLACK | MH_BTN_TEXT_GREEN);
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    switch(nID)
    {
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_BTN_RESET:
        if(GetRunState(g_knit_data) == STATE_STOP)
        {
            ShowWindow(hWnd, SW_HIDE);
            SendMessage(GetHosting(hWnd), MSG_RESET, GetKnitStartLine(g_knit_data), 0);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_BTN_HD:
        if(GetRunState(g_knit_data) == STATE_STOP)
        {
            ShowWindow(hWnd, SW_HIDE);
            SendMessage(GetHosting(hWnd), MSG_RESET, GetKnitStartLine(g_knit_data), RESET_HD);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_DROP:
        if(GetRunState(g_knit_data) == STATE_STOP)
        {
            ShowWindow(hWnd, SW_HIDE);
            SendMessage(GetHosting(hWnd), MSG_RESET, 1, RESET_DROP);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_GOTO_LINE:
        if(GetRunState(g_knit_data) == STATE_STOP)
        {
            ShowWindow(hWnd, SW_HIDE);
            SendMessage(GetHosting(hWnd), MSG_RESET, GetCurrentKnitLine(g_knit_data), RESET_GOTO);
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    switch(key)
    {
    case SCANCODE_ESCAPE:
        OnBtnCmd(hWnd, ID_ESC);
        break;
    case SCANCODE_ENTER:
        {
            OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
        }
        break;
    case SCANCODE_1:
        {
            SetFocusChild(GetDlgItem(hWnd, ID_BTN_RESET));
            OnBtnCmd(hWnd, ID_BTN_RESET);
        }
        break;
    case SCANCODE_2:
        {
            SetFocusChild(GetDlgItem(hWnd, ID_BTN_HD));
            OnBtnCmd(hWnd, ID_BTN_HD);
        }
        break;
    case SCANCODE_3:
        {
            SetFocusChild(GetDlgItem(hWnd, ID_GOTO_LINE));
            OnBtnCmd(hWnd, ID_GOTO_LINE);
        }
        break;
    case SCANCODE_4:
        {
            SetFocusChild(GetDlgItem(hWnd, ID_DROP));
            OnBtnCmd(hWnd, ID_DROP);
        }
        break;
    case SCANCODE_CURSORBLOCKDOWN:
        {
            ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
            if(nID == ID_BTN_RESET)
            {
                nID = ID_BTN_HD;
            }
            else if(nID == ID_BTN_HD)
            {
                nID = ID_GOTO_LINE;
            }
            else if(nID == ID_GOTO_LINE)
            {
                nID = ID_DROP;
            }
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        break;
    case SCANCODE_CURSORBLOCKUP:
        {
            ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
            if(nID == ID_DROP)
            {
                nID = ID_GOTO_LINE;
            }
            else if(nID == ID_GOTO_LINE)
            {
                nID = ID_BTN_HD;
            }
            else if(nID == ID_BTN_HD)
            {
                nID = ID_BTN_RESET;
            }
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        break;
    }
}
/*
 *
 */
static int ResetWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateResetInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_DMBZ]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateResetWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_DMBZ_BMP) / 2;
    int y = (CY_SCREEN - CY_DMBZ_BMP) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(706);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = ResetWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_DMBZ_BMP;
    CreateInfo.by = y + CY_DMBZ_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_RESET));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
